<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="系列名称" prop="seriesName">
        <!-- <el-select v-model="queryParams.seriesId" placeholder="请选择系列名称">
          <el-option v-for="item in seriesList" :key="item.value" :label="item.seriesName" :value="item.seriesId">
          </el-option>
        </el-select> -->
        <el-input v-model="queryParams.seriesName" placeholder="请输入系列名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="学校名称" prop="deptName">
        <el-input v-model="queryParams.deptName" placeholder="请输入学校名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['api:series:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['api:series:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['api:series:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['api:series:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="seriesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="系列名称" align="center" prop="seriesName" />
      <!-- 1展示 0隐藏 -->
      <el-table-column label="是否可用" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status" type="success">启用</el-tag>
          <el-tag v-else type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="课时" align="center" prop="classHour" />
      <el-table-column label="年龄段" align="center" prop="ages" />
      <el-table-column label="学校名称" align="center" prop="deptName" />
      <el-table-column label="封面图片" align="center" prop="coverUrl">
        <template slot-scope="scope">
          <img :src="scope.row.coverUrl" alt="" style="max-width: 100px; max-height: 100px;" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['api:series:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['api:series:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改艺术系列对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="系列名称" prop="seriesName">
          <el-input v-model="form.seriesName" placeholder="请输入系列名称" />
        </el-form-item>
        <el-form-item label="年龄段" prop="ages">
          <el-input v-model="form.ages" placeholder="请输入年龄段" />
        </el-form-item>
        <el-form-item label="课时" prop="classHour">
          <el-input v-model="form.classHour" placeholder="请输入课时" />
        </el-form-item>
        <el-form-item label="是否可用" prop="status">
          <el-radio v-model="form.status" label="1">启用</el-radio>
          <el-radio v-model="form.status" label="0">禁用</el-radio>
        </el-form-item>
        <el-form-item label="封面地址" prop="coverUrl">
          <el-upload action="http://39.98.62.132:13001/uploadFile/insert" list-type="picture-card" :multiple="true"
            :auto-upload="true" :file-list="fileList" :on-change="handleChange" :data="{ userName: 1 }"
            :on-success="handleSuccess" :before-upload="beforeUpload">
            <i slot="default" class="el-icon-plus"></i>
            <div slot="file" slot-scope="{file}">
              <img class="el-upload-list__item-thumbnail" :src="file.url" alt="">
              <span class="el-upload-list__item-actions">
                <span class="el-upload-list__item-preview" @click="handlePictureCardPreview(file)">
                  <i class="el-icon-zoom-in"></i>
                </span>
                <span v-if="!disabled" class="el-upload-list__item-delete" @click="handleDownload(file)">
                  <i class="el-icon-download"></i>
                </span>
                <span v-if="!disabled" class="el-upload-list__item-delete" @click="handleRemove(file)">
                  <i class="el-icon-delete"></i>
                </span>
              </span>
            </div>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="">
          </el-dialog>
        </el-form-item>
        <el-form-item label="学校名称" prop="deptName">
          <el-input v-model="form.deptName" placeholder="请输入学校名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSeries, getSeries, delSeries, addSeries, updateSeries } from "@/api/series/series.js";

export default {
  name: "Series",
  data() {
    return {
      //图片上传
      fileList: [], // 当前选择的文件列表
      dialogImageUrl: '',
      dialogVisible: false,
      disabled: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 艺术系列表格数据
      seriesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        seriesName: null,
        status: null,
        ages: null,
        deptId: null,
        coverUrl: null,
        createUserId: null,
        updateUserId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /* 图片上传 */
    handleChange(file, fileList) {
      // 保留最新上传的一个文件
      this.fileList = fileList.slice(-1);
    },
    beforeUpload(file) {
      if (this.fileList.length > 1) {
        this.$message.error('只能上传一个文件');
        return false; // 阻止上传
      }
      return true;
    },
    handleRemove(file) {
      // 移除文件时更新文件列表
      this.fileList = [];
      this.$refs.upload.clearFiles(); // 清除上传文件
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleDownload(file) {
      console.log(file);
    },
    handleSuccess(response) {
      // 处理上传成功的响应
      console.log('上传成功:', response);
      this.form.coverUrl = response.data;
    },
    /** 查询艺术系列列表 */
    getList() {
      this.loading = true;
      listSeries(this.queryParams).then(response => {
        this.seriesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.fileList = [];
      this.form = {
        seriesId: null,
        seriesName: null,
        status: null,
        ages: null,
        deptId: null,
        coverUrl: null,
        createTime: null,
        updateTime: null,
        createUserId: null,
        updateUserId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.seriesId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加艺术系列";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const seriesId = row.seriesId || this.ids
      getSeries(seriesId).then(response => {
        this.form = response.data;
        this.open = true;
        this.form.status = response.data.status + ""; // 转换为字符串
        if (response.data.coverUrl) {
          this.fileList.push({ url: response.data.coverUrl, status: 'success' });
        }
        this.title = "修改艺术系列";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.seriesId != null) {
            updateSeries(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSeries(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const seriesIds = row.seriesId || this.ids;
      this.$modal.confirm('是否确认删除艺术系列编号为"' + seriesIds + '"的数据项？').then(function () {
        return delSeries(seriesIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('api/series/export', {
        ...this.queryParams
      }, `series_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
